//package org.myrabbitmqdemo.product;
//
//import jakarta.annotation.PostConstruct;
//import jakarta.annotation.Resource;
//import org.myrabbitmqdemo.callback.MyConfirmCallback;
//import org.springframework.amqp.core.Message;
//import org.springframework.amqp.core.MessageProperties;
//import org.springframework.amqp.rabbit.connection.CorrelationData;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.stereotype.Component;
//
//import java.util.concurrent.ExecutionException;
//import java.util.concurrent.TimeUnit;
//import java.util.concurrent.TimeoutException;
//
///**
// * @author guangang
// */
//@Component
//public class ProductTest {
//
//    @Resource
//    private RabbitTemplate rabbitTemplate;
//
//    private final String correlationId = "sync_async_callback_demo";
//
//    @PostConstruct
//    public void init() {
//        // 设置回调监听器
//        rabbitTemplate.setConfirmCallback(new MyConfirmCallback());
//    }
//
////    @PostConstruct
////    public void sendMessageWithSyncCallback() throws InterruptedException, ExecutionException, TimeoutException {
////        CorrelationData syncCorrelation = new CorrelationData(correlationId + "_sync");
////        MessageProperties messageProperties = new MessageProperties();
////        // 设置消息ID，做幂等操作
////        //messageProperties.setHeader("messageId",correlationId);
////        messageProperties.setMessageId(correlationId);
////        Message message = new Message("Hello Sync Callback".getBytes(), messageProperties);
////        rabbitTemplate.send("my.direct", "my.direct.classic", message, syncCorrelation);
////
////        // 同步等待确认
////        boolean confirmed = syncCorrelation.getFuture().get(5, TimeUnit.SECONDS).isAck();
////        if (confirmed) {
////            System.out.println("消息已确认接收");
////        } else {
////            System.out.println("消息未被确认或超时");
////        }
////    }
//
//    /**
//     * 联邦插件测试
//     * @throws InterruptedException
//     * @throws ExecutionException
//     * @throws TimeoutException
//     */
////    @PostConstruct
////    public void sendMessageFed() throws InterruptedException, ExecutionException, TimeoutException {
////        CorrelationData syncCorrelation = new CorrelationData(correlationId + "_sync");
////        MessageProperties messageProperties = new MessageProperties();
////        // 设置消息ID，做幂等操作
////        //messageProperties.setHeader("messageId",correlationId);
////        messageProperties.setMessageId(correlationId);
////        Message message = new Message("Hello Sync Callback".getBytes(), messageProperties);
////        rabbitTemplate.send("fed_exchange", "routKey", message, syncCorrelation);
////
////        // 同步等待确认
////        boolean confirmed = syncCorrelation.getFuture().get(5, TimeUnit.SECONDS).isAck();
////        if (confirmed) {
////            System.out.println("消息已确认接收");
////        } else {
////            System.out.println("消息未被确认或超时");
////        }
////    }
//
//    // 异步等待确认
////    @PostConstruct
////    public void sendMessageWithAsyncCallback() {
////        CorrelationData asyncCorrelation = new CorrelationData(correlationId + "_async");
////        Message message = new Message("Hello Async Callback".getBytes(), new MessageProperties());
////        rabbitTemplate.send("test_queue", message, asyncCorrelation);
////    }
//
//
//        @PostConstruct
//    public void sendMessageSharding() throws InterruptedException, ExecutionException, TimeoutException {
//        CorrelationData syncCorrelation = new CorrelationData(correlationId + "_sync");
//        MessageProperties messageProperties = new MessageProperties();
//        // 设置消息ID，做幂等操作
//        //messageProperties.setHeader("messageId",correlationId);
//        messageProperties.setMessageId(correlationId);
//        Message message = new Message("Hello Sync sharding".getBytes(), messageProperties);
//            for (int i = 0; i < 100; i++) {
//                // 务必加上随机路由键，如果使用同一个路由，只会被路由到同一个分片
//                rabbitTemplate.send("sharding_exchange", String.valueOf(i),  message, syncCorrelation);
//            }
//
//}
//
//
//}
